package com.phkj.qdh.ui.dialog

import android.content.Context
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeButton
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.core.CenterPopupView
import com.phkj.qdh.R
import com.phkj.qdh.bean.Child
import com.phkj.qdh.bean.DeptUserVo
import com.phkj.qdh.ui.adapter.teamlawnode.TeamSortContentNode
import com.phkj.qdh.ui.adapter.teamlawnode.TeamSortHeadNode
import com.phkj.qdh.ui.adapter.teamlawnode.TeamTreeNodeAdapter
import com.phkj.qdh.utils.LogUtil
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.node.BaseNode
import rxhttp.wrapper.utils.GsonUtil

class TeamOrLawTreeSelectDialog(context: Context, val list: ArrayList<Child>) :
    CenterPopupView(context) {
    private var title: String = ""
    private val teamTreeNodeAdapter by lazy { TeamTreeNodeAdapter(this) }
    val nodeList = ArrayList<BaseNode>()
    fun setTitle(s: String) {
        this.title = s
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_select_man
    }

    override fun onCreate() {
        super.onCreate()
        val tvTitle = findViewById<TextView>(R.id.tv_title)
        val rv = findViewById<RecyclerView>(R.id.rv_item)
        rv.run {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(MyItemDecoration(width = 5))
            adapter = teamTreeNodeAdapter
        }
        tvTitle.text = title



        list.forEachIndexed { index1, child1 ->
            val contentList = ArrayList<BaseNode>()
            child1.child?.forEachIndexed { index2, child ->
                val content = TeamSortContentNode(index1, index2, child)
                contentList.add(content)
            }
            val head = TeamSortHeadNode(index1, child1, contentList)
            nodeList.add(head)
        }
        teamTreeNodeAdapter.setList(nodeList)
//        teamTreeNodeAdapter.setOnItemClickListener { adapter, view, position ->
//            teamTreeNodeAdapter.findParentNode(position)
//        }

        findViewById<ShapeButton>(R.id.btn_confirm).setOnClickListener {
            var data: Pair<Child?,Child?>? = null
            nodeList.forEach { head ->
                head as TeamSortHeadNode
                if (head.isSelected) {
                    data = Pair(head.head,null)
                    return@forEach
                }
                head.childNode?.forEach { child ->
                    child as TeamSortContentNode
                    if (child.isSelected) {
                        data = Pair(head.head,child.content)
                        return@forEach
                    }
                }
            }

            if (data != null) {
                listener?.ok(data!!)
                dismiss()
            } else {
                ToastUtils.show("请选择$title")
            }

        }
    }

    private var listener: SelectSortListener? = null
    fun setSelectSortListener(l: SelectSortListener) {
        listener = l
    }
}

interface SelectSortListener {
    fun ok(data:Pair<Child?,Child?>)
}